#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const inquirer = require('inquirer')
const ejs = require('ejs')

inquirer.prompt([
  {
    type: 'input',
    name: 'name',
    message: 'Project name'
  }
]).then(answers => {
  // 确定模板路径
  const tempPath = path.join(__dirname, 'templates')
  // 确定输出路径
  const outputPath = process.cwd()
  // 读取模板路径中文件
  fs.readdir(tempPath, (error, files) => {
    if(error) throw error
    files.forEach(item => {
      // 使用模板引擎将用户输入信息转换到模板中
      ejs.renderFile(path.join(tempPath, item), answers, (error, result) => {
        if(error) throw error
        // 向输出目录写入文件
        fs.writeFileSync(path.join(outputPath, item), result)
      })
    })
  })
})